//################################
// 版权所有：中国农业银行软件开发中心
// 系统名称：移动办公应用-手机版
// 文件名称：myContainer.js
// 创建时间：2016年09月20日
// 创建人员：Panda
// 功能说明：myContainer
//################################

'use strict';

import React, {Component} from 'react';
import {
    View,
    Text,
    TouchableOpacity,
    StyleSheet,
    Image,
    InteractionManager,
} from 'react-native';

import {
    Colors,
} from '../../common';
import Images from '../../images';

class MyErrorLogsItem extends Component {
    constructor(props) {
        super(props);
        
        this.isChecked = false;
        this.selectedItem = props.item;
        this.isSelectionMode = props.isSelectionMode;
    }

    componentWillReceiveProps(nextProps) {
        this.selectedItem = nextProps.item;
        this.isSelectionMode = nextProps.isSelectionMode;

        if (this.isSelectionMode === false) {
            this.isChecked = false;
        }
    }

    onSelected() {
        if (this.isSelectionMode) {
            this.isChecked = !this.isChecked;
            this.setState({});
            this.props.onPress && this.props.onPress(this.selectedItem, this.isChecked);
        }
    }

    render() {
        let text = '';
        for (let key in this.selectedItem) {
            text += key + ':' + this.selectedItem[key] + '\n';
        }

        return (
            <TouchableOpacity style={styles.item} onPress={() => {this.onSelected()}}>
                <View style={styles.itemview}>
                    {
                        this.isSelectionMode ?
                        <Image
                            style={styles.checkbox}
                            source={this.isChecked ? Images.CheckboxChecked : Images.CheckboxUnchecked}
                            resizeMode={Image.resizeMode.contain}
                        />
                        :
                        null
                    }
                    <Text style={styles.text}>{text}</Text>
                </View>
            </TouchableOpacity>
        );
    }
}

const styles = StyleSheet.create({
    item: {
        borderBottomColor: Colors.gray,
        borderBottomWidth: 0.4,
        backgroundColor: Colors.appBackground,
    },
    itemview: {
        marginTop: 6,
        marginBottom: 6,
        marginLeft: 6,
        marginRight: 6,
        flexDirection: 'row',
        flex: 1,
    },
    checkbox: {
        width: 20,
        height: 20,
        alignSelf: 'center',
        justifyContent: 'center',
        marginRight: 8,
        marginLeft: 2,
    },
    text: {
        flex: 1,
    },
});

export default MyErrorLogsItem;